﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using olierFrm.DataBase.FrameWork.DataDictornary;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace olierFrm.DataBase.FrameWork.Plugin
{
    /// <summary>
    /// 扩展方法
    /// </summary>
    public static class Extensions
    {

        #region DataTable扩展方法：数据字典绑定标题、过滤DataTable、唯一字段
        /// <summary>
        /// 绑定DataTable的标题
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable BindCaptionToDataTable(this DataTable dt)
        {
            DataDictionary.BindCaptionToDataTable(dt);
            return dt;
        }

        /// <summary>
        /// 绑定DataTable的标题
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable BindCaptionToDataTable(this DataTable dt, List<string> DicContext)
        {
            DataDictionary.BindCaptionToDataTable(dt, DicContext);
            return dt;
        }

        /// <summary>
        /// 过滤DataTable
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static System.Data.DataTable SelectToDataTable(this DataTable dt, string filter)
        {
            var dtNew = dt.Clone();
            var dtRows = dt.Select(filter);
            foreach (var r in dtRows)
            {
                dtNew.Rows.Add(r.ItemArray);
            }
            return dtNew;
        }


        /// <summary>
        /// 增加唯一字段
        /// </summary>
        /// <param name="dt"></param>
        public static void AddDataRowIndex(this DataTable dt)
        {
            string columnName = dt.GetDataRowIndexFieldName();
            dt.Columns.Add(columnName, Type.GetType("System.Int32"));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][columnName] = i;
            }
        }

        public static void AddHideColumn(this DataTable dtSource, List<string> hiddenField)
        {
            hiddenField.Remove("");
            if (hiddenField.Count == 0) return;

            if (dtSource.ExtendedProperties.ContainsKey("HideColumn"))
            {
                var dtSourceHiddenField = (List<string>)dtSource.ExtendedProperties["HideColumn"];
                foreach (var f in dtSourceHiddenField)
                {
                    if (!hiddenField.Contains(f))
                        dtSourceHiddenField.Add(f);
                }
                dtSourceHiddenField.Remove("");
            }
            else
            {
                hiddenField.Remove("");
                //hiddenField.Add("Color");
                dtSource.ExtendedProperties.Add("HideColumn", hiddenField);
            }
        }
        public static List<string> GetHideColumn(this DataTable dtSource)
        {
            if (dtSource.ExtendedProperties.ContainsKey("HideColumn"))
                return (List<string>)dtSource.ExtendedProperties["HideColumn"];
            return new List<string>();
            //foreach (var h in columnsName)
            //{
            //    if (dt.Columns.Contains(h))
            //        dt.Columns.Remove(h);
            //}
        }

        /// <summary>
        /// 获取DataTable的唯一字段名称
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetDataRowIndexFieldName(this DataTable dt)
        {
            if (dt == null) return null;
            return dt.TableName + "_DataRowIndex";
        }

        #endregion

        #region 主要针对DataObject
        /// <summary>
        /// 增加UpdateFlag，主要针对DataObject
        /// </summary>
        /// <param name="dt"></param>
        public static void AddUpdateFlag(this DataTable dt)
        {
            dt.Columns.Add(new DataColumn("UpdateFlag", Type.GetType("System.String")));
        }

        /// <summary>
        /// 获取UpdateFlag字段的名称，主要针对DataObject
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string GetUpdateFlagFildName(this DataTable dt)
        {
            return "UpdateFlag";
        }
        #endregion


        /// <summary>
        /// 被克隆的类必须标记为可序列化
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public static object Clone(this object t) { return (object)CloneObject(t); }
        private static object CloneObject(object obj)
        {
            using (MemoryStream memStream = new MemoryStream())
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
                binaryFormatter.Serialize(memStream, obj); memStream.Seek(0, SeekOrigin.Begin);
                return binaryFormatter.Deserialize(memStream);
            }
        }


        public static string ParseSQL(this string strSql)
        {
            //new DBFrm.ParseSQL().
            return "";
        }
    }
}
